config = require '../config'
resErr = require('./util').resErr
md5 = require('../lib/utils').md5
SysUser = require('../models/sys_user').SysUser

exports.login_ownpwd_admin = (req,res) ->
  password = req.param("password")
  user_id = req.param("user_id")
  return resErr res,'password不得为空' if !password
  return resErr res,'user_id不得为空' if !user_id
  password = md5(password)
  SysUser.findOne {username:user_id,password:password},(err, result) ->
    return resErr res,err if err
    #cache_token_session(req,result)
    if result
      req.session.admin_user = result
    else
      return resErr res,'用户名或密码错误！'
    return res.json {success:true}

cache_token_session = (req,token) ->
  if token?.login_type == "oa"
    req.session.admin_token = {}
    req.session.admin_token.access_token = token.access_token
    req.session.admin_token.refresh_token = token.refresh_token
    req.session.admin_token.expires_in = token.expires_in
    req.session.admin_token.user_id = token.user_id
    req.session.admin_token.scope = token.scope

